﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form

    {
        /// <summary>
        /// 父类Animal
        /// </summary>
        public class Animal
        {
            public string Name;

            public void setName(String name)
            {
                this.Name = name;
            }

            public String jiao()
            {
                return "嗷呜";
            }
        }
        /// <summary>
        /// 子类cat
        /// </summary>
        public class cat : Animal
        {
            // 多态
            public String jiao(String type)
            {
                if (type == "cat")
                    return "喵喵喵";
                else
                    return "汪汪汪";
            }
        }
        /// <summary>
        /// 接口
        /// </summary>
        interface Dog
        {
            String jiao();
            String jiao(int times);
            String jiao(String msg);
        }
        /// <summary>
        /// 实现类
        /// </summary>
        class DogImpl : Dog
        {
            public String jiao()
            {
                return "汪";
            }
            public String jiao(int times)
            {
                String text = "";
                for (int i = 0; i < times; i++)
                {
                    text += "汪";
                }
                return text;
            }
            public String jiao(String msg)
            {
                return msg;
            }
        }

        /// <summary>
        /// 异常处理
        /// </summary>
        public class cal
        {
            public String division(int num1, int num2)
            {
                float ans = 0;
                try
                {
                    ans = num1 / num2;
                }
                catch (DivideByZeroException e)
                {
                    return e.ToString();
                }
                catch (OutOfMemoryException e)
                {
                    return e.ToString();
                }
                finally
                {
                    ;
                }
                return ans.ToString();
            }
        }

        class A
        {
            public String aa()
            {
                B bb = new B();
                try
                {
                    bb.bb();
                }
                catch (Exception e)
                {
                    return e.ToString();
                }
                return "";
            }
        }

        class B
        {
            int i = 1;
            public void bb()
            {
                if (i != 0)
                {
                    throw new Exception("电脑故障了，i不可能大于0.");
                }
            }
        }

        /// <summary>
        /// 单例模式
        /// </summary>
        public class singleTon
        {
            private static singleTon instance;
            private singleTon() { }

            public static singleTon getInstance()
            {
                if (instance == null)
                {
                    instance = new singleTon();
                }
                return instance;
            }
        }

        /// <summary>
        /// 工厂模式
        /// </summary>
        public abstract class computer
        {
            public abstract String print();
        }

        public class DellComputer : computer
        {
            public override String print()
            {
                return "这是一台戴尔电脑。";
            }
        }

        public class ROGComputer : computer
        {
            public override String print()
            {
                return "这是一台ROG电脑。";
            }
        }

        /// <summary>
        /// 工厂
        /// </summary>
        public class ComputerFactory
        {
            private computer computer = null;
            public computer CreateComputer(String type)
            {
                switch (type)
                {
                    case "Dell":
                        computer = new DellComputer();
                        break;
                    case "ROG":
                        computer = new ROGComputer();
                        break;
                    default:
                        break;
                }
                return computer;
            }
        }

        /// <summary>
        /// 抽象工厂类
        /// </summary>
        public abstract class BallFactory
        {
            public abstract Ball createBall();
            public abstract Pin createBallPin();
        }

        /// <summary>
        /// 篮球
        /// </summary>
        public class BasketballFactory : BallFactory
        {
            public override Ball createBall()
            {
                return new BasketBall();
            }
            public override Pin createBallPin()
            {
                return new BacketballPin();
            }
        }

        /// <summary>
        /// 足球
        /// </summary>
        public class FootballFactory : BallFactory
        {
            public override Ball createBall()
            {
                return new Football();
            }
            public override Pin createBallPin()
            {
                return new FootballPin();
            }
        }

        public abstract class Ball
        {
            public abstract String print();
        }

        public abstract class Pin
        {
            public abstract String print();
        }

        public class BasketBall : Ball
        {
            public override String print()
            {
                return "这是篮球";
            }
        }

        public class BacketballPin : Pin
        {
            public override String print()
            {
                return "这是篮球针";
            }
        }

        public class Football : Ball
        {
            public override String print()
            {
                return "这是足球";
            }
        }

        public class FootballPin : Pin
        {
            public override String print()
            {
                return "这是足球针";
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        public abstract class Body
        {
            public abstract String Print();
        }

        public class Man : Body
        {
            public override String Print()
            {
                return "男人";
            }
        }

        public class Woman : Body
        {
            public override String Print()
            {
                return "女人";
            }
        }

        public abstract class Decorator : Body
        {
            public abstract override String Print();
        }

        public class Clothes : Decorator
        {
            private readonly Body _body;

            public Clothes(Body body)
            {
                _body = body;
            }

            public override String Print()
            {
                return "穿上衣服";
            }
        }

        public class Shoes : Decorator
        {
            private readonly Body _body;

            public Shoes(Body body)
            {
                _body = body;
            }

            public override String Print()
            {
                return "穿上鞋子";
            }
        }

        public class Pants : Decorator
        {
            private readonly Body _body;

            public Pants(Body body)
            {
                _body = body;
            }

            public override String Print()
            {
                return "穿上裤子";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String text = "";
            // 继承多态
            cat obj1 = new cat();
            text += obj1.jiao();
            text += "\n";
            text += obj1.jiao("cat");
            text += "\n";
            // 接口多态
            DogImpl obj2 = new DogImpl();
            text += obj2.jiao();
            text += "\n";
            text += obj2.jiao(5);
            text += "\n";
            text += obj2.jiao("哼，哼，哼");
            text += "\n";
            // 异常处理
            cal obj3 = new cal();
            text += obj3.division(1, 2);
            text += "\n";
            text += obj3.division(1, 0);
            text += "\n";
            // 向上抛出异常
            A obj4 = new A();
            text += obj4.aa();
            text += "\n";
            // 支持泛型的栈
            Stack<int> stack = new Stack<int>();
            stack.Push(1);
            text += stack.Pop().ToString();
            text += "\n";
            text += stack.Count.ToString();
            text += "\n";
            // 泛型队列
            Queue<int> queue = new Queue<int>();
            queue.Enqueue(1);
            text += queue.Dequeue().ToString();
            text += "\n";
            text += queue.Count.ToString();
            text += "\n";
            // 单例模式
            singleTon obj5 = singleTon.getInstance();
            // 工厂模式
            ComputerFactory computerFactory = new ComputerFactory();
            computer com1 = computerFactory.CreateComputer("Dell");
            computer com2 = computerFactory.CreateComputer("ROG");
            text += com1.print();
            text += "\n";
            text += com2.print();
            text += "\n";
            // 抽象工厂模式
            BallFactory basketballFactory = new BasketballFactory();
            Ball basketball = basketballFactory.createBall();
            text += basketball.print();
            text += "\n";
            Pin basketballPin = basketballFactory.createBallPin();
            text += basketballPin.print();
            text += "\n";
            BallFactory footballFactory = new FootballFactory();
            text += footballFactory.createBall().print();
            text += "\n";
            text += footballFactory.createBallPin().print();
            text += "\n";
            // 装饰器模式
            Body man = new Man();
            text += man.Print();
            text += "\n";
            man = new Shoes(man);
            text += man.Print();
            text += "\n";
            man = new Pants(man);
            text += man.Print();
            text += "\n";
            man = new Clothes(man);
            text += man.Print();
            text += "\n";
            this.label1.Text = text;
            
        }
    }
}
